package hn.cch;

import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class AppMain {

    private static Logger logger = LoggerFactory.getLogger(AppMain.class);

    public static final String USER_DIR = System.getProperty("user.dir");

    public static final String ETC_DIR = USER_DIR + File.separator + "etc";

    public static final String LOG_PATH = ETC_DIR + File.separator + "log.properties";

    public static final String RCN_PATH = ETC_DIR + File.separator + "rcn.properties";

    public static final String redis_cluster_host = "redis.cluster.host";
    public static final String redis_cluster_port = "redis.cluster.port";

    public static final String redis_conn_timeout = "redis.conn.timeout";
    public static final String redis_read_timeout = "redis.read.timeout";

    public static final String redis_fail_maxtimes = "redis.fail.maxtimes";
    public static final String redis_auth_password = "redis.auth.password";


    public static void main(String[] args) {
        try {
            // 日志配置文件
            PropertyConfigurator.configure(LOG_PATH);

            // redis cluster 配置文件
            Properties properties = new Properties();
            File file = new File(RCN_PATH);
            properties.load(new FileInputStream(file));

            //连接池配置
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxTotal(1);
            jedisPoolConfig.setMaxIdle(1);
            jedisPoolConfig.setMaxWaitMillis(1000);


            // 以|作为分隔符
            String[] hosts = properties.getProperty(redis_cluster_host).split("\\|");
            String[] ports = properties.getProperty(redis_cluster_port).split("\\|");

            if (hosts.length != ports.length) {//配置文件校验
                logger.error("length hosts != ports");
                return;
            }

            //哨兵模式集群部署
            Set<HostAndPort> jedisClusterNode = new HashSet<>();
            for (int i = 0; i < hosts.length; i++) {
                jedisClusterNode.add(new HostAndPort(hosts[i], Integer.parseInt(ports[i])));
            }

            //连接和读取超时时间
            int connTimeout = Integer.parseInt(properties.getProperty(redis_conn_timeout));
            int readTimeout = Integer.parseInt(properties.getProperty(redis_read_timeout));

            //失败重试次数
            int maxtimes = Integer.parseInt(properties.getProperty(redis_fail_maxtimes));

            //密码
            String password = properties.getProperty(redis_auth_password);

            //Java连接Redis集群//int connectionTimeout, int soTimeout, int maxAttempts, String password,
            JedisCluster jedisCluster = new JedisCluster(jedisClusterNode, connTimeout, readTimeout, maxtimes, password, jedisPoolConfig);


            logger.info(jedisCluster.set(redis_cluster_host, redis_cluster_port));
            logger.info(redis_cluster_host + ":" + jedisCluster.get(redis_cluster_host));

            // Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();

            //释放客户端
            jedisCluster.close();

        } catch (Exception e) {
            logger.error("redis error :{}", e.getMessage());
            e.printStackTrace();
        }


    }

}
